//index.js
//获取应用实例
const app = getApp()
var wh = require('../../utils/wh.js');
var e = require('../../wxParse/wxParse.js');
const innerAudioContext = wx.createInnerAudioContext()
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    route: "home",
    icons: '',
    shop: {},
    pagedata: {},
    startadv: {},
    closestartadv: 0,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    circular: true,
    storeRecommand: [],
    total: 0,
    page: 1,
    loaded: !1,
    loading: !0,
    indicatorDotsHot: !1,
    autoplayHot: !0,
    intervalHot: 5e3,
    durationHOt: 1e3,
    circularHot: !0,
    audioplay: false,//默认不自动播放
    setting: {},
    forminfo: '',
    noticetxt: "公告标题",
    rootpath: 'http://app.zhongzivt.com'
  },
  setintvalid: null,
  onLoad: function (options) {
    let that = this;
    this.get_setting();
    this.getHomePage();
  },
  //下拉刷新
  onPullDownRefresh: function () {
    let that = this;
    this.get_setting();
    this.getHomePage();
    wx.stopPullDownRefresh();

  },
  //用户点击右上角分享
  onShareAppMessage: function () {

  },
  //加载首页模板页面
  getHomePage: function () {
    var that = this;
    var seckill;
    //加载数据
    wh.Post('/api/page/index', {}, function (res) {
      //设置title
      if (res.page.title) {
        wx.setNavigationBarTitle({ title: res.page.title })
      }
      //设置title背景
      if (res.page.titlebarbg) {
        wx.setNavigationBarColor({ frontColor: res.page.titlebarcolor, backgroundColor: res.page.titlebarbg })
      }
      var htmlindex = 0;
      //循环items
      for (var i in res.items) {
        switch (res.items[i].id) {
          //处理接口返回的icon样式不对问题
          case 'fixedsearch':
            if (res.items[i].params.leftnavicon != '') res.items[i].params.leftnavicon = res.items[i].params.leftnavicon.replace('icon', 'icox');
            if (res.items[i].params.rightnavicon != '') res.items[i].params.rightnavicon = res.items[i].params.rightnavicon.replace('icon', 'icox');
            break;
          //处理富文本
          case 'richtext':
            e.wxParse("htmlcontent" + htmlindex, "html", res.items[i].params.content, that, "5");
            res.items[i].params.htmlindex = htmlindex;
            htmlindex++;
            break;
          //处理视频高度
          case 'video':
            wx.getSystemInfo({
              success: function (st) {
                var videow = st.windowWidth;
                var hei = videow;
                if (res.items[i].style.ratio == 1) hei = videow * 3 / 4;
                else if (res.items[i].style.ratio == 0) hei = videow * 9 / 16;
                res.items[i].style.height = hei;
              }
            })
            break;
          //处理公告跑马灯
          case 'notice':
            let noticetxt = ''; let k = 1;
            for (var j in res.items[i].data) {
              noticetxt += res.items[i].data[j].title + "。   ";
            }
            that.setData({ noticetxt: noticetxt });
            break;
        }
      }
      //处理富文本
      if (htmlindex > 0) e.wxParseTemArray("wxParseData", 'htmlcontent', htmlindex, that);
      //变量赋值
      that.setData({ pagedata: res, startadv: {}, loading: 0, show: 1 })
    })
  },
  //设置banner图片自适应高度
  imagesHeight: function (t) {
    var a = t.detail.width,
      e = t.detail.height,
      o = t.target.dataset.type,
      i = {},
      s = this;
    wx.getSystemInfo({
      success: function (t) {
        i[o] = t.windowWidth / a * e,
          (!s.data[o] || s.data[o] && i[o] < s.data[o]) && s.setData(i)
      }
    })
  },
  //获取小程序设置
  get_setting: function () {
    let that = this;
    wh.Post('/api/setting/get', {}, function (res) {
      that.setData({ setting: res });
      that.audio_setting();//获取成功后返回
    })
  },
  //拨打电话
  call: function () {
    let that = this;
    let setting = that.data.setting;
    let tell = setting.service_tel;
    if (tell == '' || tell == undefined) {
      wx.showModal({
        title: '提示', content: '哎呀，还未设置电话号码呢，请稍后再试', showCancel: false
      })
    }
    wx.makePhoneCall({ phoneNumber: tell })
  },
  //地图导航
  map: function () {
    var that = this;
    let setting = that.data.setting;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度  
      success: function (res) {
        var latitudeIdx = Number(setting.lat);
        var longitudeIdx = Number(setting.lng);
        var map_name = setting.map_name;
        wx.openLocation({
          latitude: latitudeIdx, longitude: longitudeIdx, name: map_name, scale: 15
        })
      }
    })
  },
  //音乐设置
  audio_setting: function () {

    let setting = this.data.setting;
    if (setting.audio_open != 1) return;
    let auto = setting.audio_auto == 1 ? true : false;
    if (auto) this.setData({ audioplay: true });//播放模式
    innerAudioContext.autoplay = auto;
    innerAudioContext.play();
    innerAudioContext.src = setting.audio;
    innerAudioContext.onPlay(() => { })
    innerAudioContext.onStop(() => { });
  },
  //音乐点击操作
  audioClick: function () {
    var that = this;
    if (that.data.audioplay) {
      innerAudioContext.pause();
      that.setData({
        audioplay: !that.data.audioplay,
        music: 'close-music'
      })
    } else {
      innerAudioContext.play();
      that.setData({
        audioplay: !this.data.audioplay,
        music: 'music'
      })
    }
  },
  // 提交留言信息
  formSubmit: function (e) {

    let that = this;
    let name = e.detail.value.name;
    let mobile = e.detail.value.mobile;
    let content = e.detail.value.conetnt;
    if (name == '') {
      wx.showToast({ title: '请输入你的姓名', icon: 'none' });
      return;
    }
    if (mobile == '') {
      wx.showToast({ title: '请输入你的手机号', icon: 'none' });
      return;
    }
    if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(mobile))) {
      wx.showToast({ title: '请填写有效的手机号', icon: 'none' });
      return;
    }
    if (content == '') {
      wx.showToast({ title: '请输入留言内容', icon: 'none' });
      return;
    }
    wh.Post('/api/common.form/contact', { name: name, mobile: mobile, content: content }, function (res) {
      wx.showToast({ title: '信息提交成功', icon: 'none' });
      that.setData({ forminfo: '' });
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.app_auth(this)
  },
})